package Offer.offer;

import java.util.Stack;

/**
 * 栈的压入、弹出序列
 *
 * @author leiwenkang
 * @date 2020/4/6
 */
public class Test22栈的压入弹出序列 {
    public static void main(String[] args) {
        int[] pushArray={1,2,3,4,5};
        int[] popArray={4,3,5,2,1};
//        int[] pushArray={2,1,0};
//        int[] popArray={1,2,0};
        System.out.println(isPopOrder(pushArray,popArray));
    }

    public static boolean isPopOrder(int[] pushArray, int[] popArray) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < popArray.length; i++) {
            while (j < pushArray.length && (stack.isEmpty() || stack.peek() != popArray[i])) {
                stack.push(pushArray[j]);
                j++;
            }

            if (stack.peek() == popArray[i]) {
                stack.pop();
            }
        }

        return stack.isEmpty();
    }
}
